package org.nf.product.dao.impl;

import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanListHandler;
import org.nf.db.util.result.ColumnHandler;
import org.nf.product.dao.ProductDAO;
import org.nf.product.model.Product;
import org.nf.product.util.ConnUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 产品dao 接口实现类
 */
public class ProductDaoImpl implements ProductDAO {

    /**
     * 添加全部产品
     * @param product
     * @return
     */
    @Override
    public int insertProduct(Product product) {
        //实例化SqlExecutor对象 提供添加操作
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql="insert into design_product(product_name,product_price,product_details,product_date,type_id,product_image,product_area,stylist_name,product_sales) values (?,?,?,?,?,?,?,?,?)";

        return executor.executeUpdate(sql,product.getProductName(),product.getProductPrice(),product.getProductDetails(),product.getProductDate(),product.getType_id(),product.getProductImage(),product.getProductArea(),product.getStylistName(),product.getProductSales());
    }

    /**
     * 通过编号删除产品
     * @param productId
     * @return
     */
    @Override
    public int deleteProduct(int productId) {
        //实例化SqlExecutor对象 提供删除操作
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql="delete from design_product where product_id=? ";
        return executor.executeUpdate(sql,productId);
    }

    /**
     * 修改全部产品
     * @param product
     * @return
     */
    @Override
    public int updateProduct(Product product) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql="update design_product set product_name=?, product_price=?,product_details=?,product_date=?,type_id=?,product_image=?,product_area=?,stylist_name=?,product_sales=? where= product_id=?";
        return executor.executeUpdate(sql,product.getProductName(),product.getProductPrice(),product.getProductDetails(),product.getProductDate(),product.getType_id(),product.getProductImage(),product.getProductArea(),product.getStylistName(),product.getProductSales());

    }

    /**
     * 通过名称查询产品
     * @param productName
     * @return
     */
    @Override
    public List<Product> selectProductName(String productName) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        BeanListHandler<Product> handler = new BeanListHandler<>(Product.class);
        String sql="select product_id,product_name,product_price,product_details,product_date,type_id,product_image,product_area,stylist_name,product_sales from design_product where product_name LIKE '%'?'%'";
        return executor.executeQuery(sql,handler,productName);
    }

    /**
     * 分页查询产品
     * @param page 从第几条开始查询
     * @param limit 取多少条
     * @return
     */
    @Override
    public List<Product> listProductPage(Product product, int page, int limit) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        //定义一个集合，存储动态sql中的占位符
        List<Object> params = new ArrayList<>();
        //用于sql拼接
        StringBuilder sql = new StringBuilder("select product_id,product_name");
        sql.append(" product_price,product_details,product_date,type_id,product_image");
        sql.append(" product_area,stylist_name,product_sales");
        sql.append(" from design_product where 1=1");

        if (product !=null && product.getProductName()!=null && !"".equals(product.getProductName().trim())){
            sql.append(" and product_name like '%'?'%'");
            params.add(product.getProductName());
        }
        sql.append("limit ?,?");
        params.add((page - 1 )*limit);
        params.add(limit);


        BeanListHandler<Product> handler = new BeanListHandler<>(Product.class);
        return executor.executeQuery(sql.toString(), handler,params.toArray() );
    }

    /**
     * 查询全部产品
     * @return
     */
    @Override
    public List<Product> listProduct(Product product) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        BeanListHandler<Product> handler = new BeanListHandler<>(Product.class);
        String sql="select product_id,product_name,product_price,product_details,product_date,type_id,product_image,product_area,stylist_name,product_sales from design_product";

        return executor.executeQuery(sql,handler);
    }

    /**
     * 总记录数，用于计算总页数
     * @return
     */
    @Override
    public Long countProduct() {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "select count(*) from design_product";
        ColumnHandler<Long> handler = new ColumnHandler<>(1);
        return executor.executeQuery(sql, handler);
    }

}
